import {Component, OnInit, Output, EventEmitter} from "@angular/core";
import {UserModel} from "../../model/user.model";
import {ROUTE} from "../../service/route.service";
import {USER} from "../../service/user.service";
import {CONSTANT} from "../../service/constant.service";

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnInit {

  @Output('cityChanged') public cityChanged = new EventEmitter();
  showLoginModal:boolean = false;
  isShowQrCode:boolean = false;
  cityList:any = [{code: 310100, name: '上海'}, {code: 330100, name: '杭州'}];
  city:any = {code: 310100, name: '上海'};
  modalContent:string = 'login';
  user:UserModel;
  navLists:any = this.constant.navBarList;
  constructor(private route:ROUTE,
              private USER:USER,
              private constant:CONSTANT) {

    this.user = USER.get();
  }

  ngOnInit() {
    this.USER.refreshUser.subscribe(v=> {
      console.log("USER.refresh");
      this.user = this.USER.get();
    })
  }

  login() {
    this.route.goLoginView()
  }

  showModal(content:any) {
    this.modalContent = content;
    this.showLoginModal = true;
  }

  closeLogin(result:Object) {
    if (result instanceof UserModel) {
      this.user = result;
      this.USER.update(result);
    }
    this.showLoginModal = false;
    console.log(this.user);
  }

  logout() {
    this.user = null;
    this.USER.reset();
    this.route.goHomeView();
  }

  showQrCode() {
    this.isShowQrCode = true;
  }

  closeQrCode() {
    this.isShowQrCode = false;
  }

  switchCity() {
    if (this.city.code == this.cityList[0].code) {
      this.city = Object.assign({}, this.cityList[1]);
    } else {
      this.city = Object.assign({}, this.cityList[0]);
    }
    this.cityChanged.emit(this.city);
  }

}
